package com.wsoft.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.annotation.InitRedisCache;
import com.wsoft.core.exception.Asserts;
import com.wsoft.system.dto.SysPostDTO;
import com.wsoft.system.entity.SysPostEntity;
import com.wsoft.system.mapper.SysPostMapper;
import com.wsoft.system.query.post.SysPostReq;
import com.wsoft.system.service.ISysPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 岗位 服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年09月11日 15:05:53
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPostEntity> implements ISysPostService {

    @Override
    @InitRedisCache(type = RedisKeyConstant.POST_ALL_CACHE,
                    value = RedisKeyConstant.REDIS_NULL)
    public boolean insert(SysPostDTO dto) {
        SysPostEntity entity = Convert.convert(SysPostEntity.class, dto);
        QueryWrapper<SysPostEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysPostEntity::getCode, entity.getCode());
        if (count(query) > 0) {
            Asserts.fail("[sys.code.exist]");
        }
        return save(entity);
    }

    @Override
    @InitRedisCache(types = {RedisKeyConstant.POST_CACHE,RedisKeyConstant.POST_ALL_CACHE}
                ,values = {"id",RedisKeyConstant.REDIS_NULL})
    public boolean update(SysPostDTO dto) {
        SysPostEntity entity = Convert.convert(SysPostEntity.class, dto);
        QueryWrapper<SysPostEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysPostEntity::getCode, entity.getCode())
                .ne(SysPostEntity::getId, entity.getId());
        if (count(query) > 0) {
            Asserts.fail("[sys.code.exist]");
        }
        return updateById(entity);
    }

    @Override
    @GetRedisCache(type = RedisKeyConstant.POST_CACHE)
    public SysPostEntity getPost(Long id) {
        return getById(id);
    }

    @Override
    @InitRedisCache(types = {RedisKeyConstant.POST_CACHE,RedisKeyConstant.POST_ALL_CACHE})
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public PageInfo<SysPostEntity> pagePost(SysPostReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        QueryWrapper<SysPostEntity> query = new QueryWrapper<>();
        //岗位类型
        if (StrUtil.isNotBlank(req.getType())) {
            query.lambda().eq(SysPostEntity::getType, req.getType());
        }
        //编码
        if (StrUtil.isNotBlank(req.getCode())) {
            query.lambda().eq(SysPostEntity::getCode, req.getCode());
        }
        //名称
        if (StrUtil.isNotBlank(req.getName())) {
            query.lambda().like(SysPostEntity::getName, req.getName());
        }
        return PageInfo.of(list(query));
    }
}
